<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
<div class="container-fluid am-margin-top">
<form id="${uuid}_form">
	<input type="hidden" name="id">
	<input type="hidden" name="tableId">
	<input type="hidden" name="sn">
	
    
    <div class="row">
    	<div class="col-sm-6">
    		<div class="form-group">
			  	<label>字段描述</label>
		     	<input type="text" name="fieldName" check-type="required" class="form-control">
			  </div>
    	</div>
    	<div class="col-sm-6">
    		<div class="form-group">
			  	<label>字段名</label>
		     	<input type="text" name="originalName" check-type="required" class="form-control">
			  </div>
    	</div>
	</div>
	
	<div class="row">
		<div class="col-sm-6">
			<div class="form-group">
			  	<label>字段类型</label>
		     	<select name="fieldType" class="form-control">
		             <pf:dataDic var="item" dicCode="TABLE_DEF_FIELD_TYPE">
		                 <option value="${item.value}">${item.name}</option>
		             </pf:dataDic>
		         </select>
			  </div>
		</div>
		<div class="col-sm-6">
			<label>长度/格式</label>
		  	<div>
	     	<span class="fieldTypeSetting" id="fieldTypeSetting_varchar" style="display: none;">
	     		<input type="text" name="charLen" value="255" class="form-control">
	     	</span>
	     	<span class="fieldTypeSetting" id="fieldTypeSetting_number" style="display: none;">
	     		整数位：<input type="text" name="intLen" value="13" class="am-input-sm" style="display: inline;width: 40px">
	     		小数位：<input type="text" name="decimalLen" value="0" class="am-input-sm" style="display: inline;width: 40px">
	     	</span>
	     	<span class="fieldTypeSetting" id="fieldTypeSetting_date" style="display: none;">
	     		<input type="text" name="dateFormat" value="yyyy-MM-dd" class="form-control">
	     	</span>
	     	<span class="fieldTypeSetting" id="fieldTypeSetting_dateTime" style="display: none;">
	     		<input type="text" name="dateTimeFormat" value="yyyy-MM-dd HH:mm:ss" class="form-control" >
	     	</span>
	     	</div>
		</div>
	</div>
	
	
	<div class="row">
		<div class="col-sm-6">
			<div class="form-group">
			  	<label>选项</label>
		     	<div>
		     		<span class="am-margin-left-sm"><input type="checkbox" name="isRequired" value="1">必填</span>
		     		<span class="am-margin-left-sm"><input type="checkbox" name="isFlowvar" value="1">流程变量</span>
		     		<span class="am-margin-left-sm"><input type="checkbox" name="isHidden" value="1">隐藏</span>
		     	</div>
		     	<div>
		     		<span class="am-margin-left-sm"><input type="checkbox" name="isDefaultTime" value="1">默认时间</span>
		     	</div>
			</div>
		</div>
		<div class="col-sm-6">
			<div class="form-group">
			  	<label>默认值</label>
		     	<input type="text" name="defaultVal" class="form-control" >
			  </div>
		</div>
	  
	</div>
	
	
	
	<div class="row">
		<div class="col-sm-6">
			<div class="form-group">
		  	<label>控件类型</label>
	     	<select name="controlType" class="form-control" >
	             <pf:dataDic var="item" dicCode="TABLE_DEF_CTR_TYPE">
	                 <option value="${item.value}">${item.name}</option>
	             </pf:dataDic>
	         </select>
		  </div>
		</div>
		<div class="col-sm-6">
			<div class="form-group">
		  	 <div class="controlTypeSetting" id="controlTypeSetting_dataDicCode">
				 <label>字典编码</label>
			     <input type="text" name="dataDicCode" class="form-control" >
		     </div>
		  </div>
		</div>
	</div>
	
	<div class="row controlTypeSetting" id="controlTypeSetting_options">
	  <div class="col-sm-12 am-form-group">
	  	<label>选项</label>
     	<textarea name="options"  class="form-control"></textarea>
     	<p class="help-block">格式:[{'title':'','value':'','isDefault':'true'},{'title':'','value':''}]</p>
     	
	  </div>
	</div>
	
	
	
   
   	<div class="form-group">
   		<a href="javascript:void(0);" id="${uuid}_submitBt" class="btn btn-primary pull-right">提交</a>
   	</div>
</form>
</div>

<script type="text/javascript">
$(function(){
	var id = '${param.id}';
	var tableId='${param.tableId}';
	var uuid = '${uuid}';
	var form = $("#" + uuid + "_form");
	var formValidation=form.validation();
	
	$("#" + uuid + "_submitBt").click(function(){
		if (isValid()) {
			form.ajaxSubmit({
				url : ctx + '/formFieldDefineAction/saveFieldDef.do',
				data : {},
				type : "POST",
				success : function(data) {
					if (data.success) {
						webUtils.alert("提交成功");
						top.webUtils.topicPublish(
								"fieldDefine.edit.success", data);
					} else {
						webUtils.error(data.msg);
					}
				}
			});
		}
	});
	
	function isValid(){
    	return formValidation.valid();
    }
	
	form.find("select[name=fieldType]").change(function(){
		parseFieldType();
	});
	
	form.find("select[name=controlType]").change(function(){
		parseControlType();
	});
	
	form.find("input[name=fieldName]").blur(function(){
		var $this=$(this);
		var originalName=form.find("input[name=originalName]")
		if(!originalName.val()){
			webUtils.getShortPinyin($this.val(),function(str){
				originalName.val(str);
			});
		}
	});
	
	var init=function(){
		parseFieldType();
		parseControlType();
	}
	
	var parseControlType=function(){
		$(".controlTypeSetting").hide();
		var controlType=form.find("select[name=controlType]").val();
		var r=[];
		if(controlType=='dictionary' || controlType=='dropdownList'  || controlType=='chkbox' || controlType=='radio'){
			$("#controlTypeSetting_dataDicCode").show();
		}
		
		if(controlType=='dropdownList'  || controlType=='chkbox' || controlType=='radio'){
			$("#controlTypeSetting_options").show();
		}
		
		
	}
	
	var parseFieldType=function(){
		$(".fieldTypeSetting").hide();
		var fieldType=form.find("select[name=fieldType]").val();
		$("#fieldTypeSetting_"+fieldType).show();
	}
	
	if (id) {
		$.getJSON(ctx + '/formFieldDefineAction/findFieldDefById.do',{id : id},function(data){
			webUtils.fullForm({
				form : form,
				data : data
			});
			
			init();
		});
	}else{
		form.find("input[name=tableId]").val(tableId);
		init();
	}
	
	
	
});
</script>